<?php



// +----------------------------------------------------------------------



// | OneThink [ WE CAN DO IT JUST THINK IT ]



// +----------------------------------------------------------------------



// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.



// +----------------------------------------------------------------------



// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>



// +----------------------------------------------------------------------







namespace Home\Controller;



use Think\Controller;







/**



 * 前台公共控制器



 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称



 */



class HomeController extends Controller {







	/* 空操作，用于输出404页面 */



	public function _empty(){



		$this->redirect('Index/index');



	}











    protected function _initialize(){



        /* 读取站点配置 */

    // A('Home://CourseRecord') ->autoConfirm();

        $config = api('Config/lists');



        C($config); //添加配置







        if(!C('WEB_SITE_CLOSE')){



            $this->error('站点已经关闭，请稍后访问~');



        }








$this ->autoConfirm();
        if (!session('uid')) {
            $post['mobile'] = $_COOKIE['username'];
            $post['password'] = $_COOKIE['password'];
            $post['remember'] = $_COOKIE['remember'];
            if (!empty($post['mobile'])) {
                session('re_url',$_SERVER['HTTP_REFERER']);
                $data = D('UserMember')->showOne($post);
                if($data == NULL){
					//lkl处理自动退出
					D('UserMember')->updateLogin('id', session('uid'), 0);
					session(null);
					
					setcookie('username', '', time() - 3600 * 24 * 365);

					setcookie('password', '', time() - 3600 * 24 * 365);
					
					
                    $this ->error('用户名不存在',U('UserMember/login'));
                }
                $data = D('UserMember') ->contrastPwd($post);
                if($data == NULL){
                    setcookie('username',$user['username'],time()-3600*24*365);
                    setcookie('password',$post['password'],time()-3600*24*365);
                    setcookie('remember',$post['remember'],time()-3600*24*365);
                    $this ->error('密码不正确',U('Index/index'));
                }else{
                    session('uid',$data['id']);
                    $user = D('UserMember') -> showUser(session('uid'));
//                    cho "<pre>";

//                    var_dump($user);       
//                    exit();
                    //记住密码
                    if (I('post.remember') == 1) {
                        setcookie('username',$user['username'],time()+3600*24*365);
                        setcookie('password',$post['password'],time()+3600*24*365);
                        setcookie('remember',$post['remember'],time()+3600*24*365);
                    }
                    //判断是否是同一天登录
                    if (date('ymd',$user['last_login_time']) != date('ymd',time())) {

                        //如果不是，更新登录时间，查看、自荐次数
                        $update['id'] = session('uid');
                        $update['last_login_time'] = time();



                        $update['checked'] = 0;



                        $update['messages'] = 0;



                        D('UserMember') -> updatePost($update);



                    }



                }



            }



        }







        //登录控制



        if (ACTION_NAME != 'userlist' && ACTION_NAME != 'userdetail' && ACTION_NAME != 'verify' && ACTION_NAME != 'login' && ACTION_NAME != 'register' && ACTION_NAME != 'commonlist' && ACTION_NAME != 'arealist' && ACTION_NAME != 'ajaxyzm' && ACTION_NAME != 'forgetpwd' && ACTION_NAME != 'agreement' && ACTION_NAME != 'jubao' && !session('uid') && ACTION_NAME != 'codes' && ACTION_NAME != 'wx_openid') {



               $this -> logins();



        }



        



       



        if(ACTION_NAME != 'login'){

            if(D('UserMember') ->updateLogin('id',session('uid')) != session('is_login')){
				
			/* lkl修复，先执行退出，再执行跳转。 */
			D('UserMember')->updateLogin('id', 0);

			session(null);

			//检查是否成功

			if (!session('?uid')) {

				setcookie('username', '', time() - 3600 * 24 * 365);

				setcookie('password', '', time() - 3600 * 24 * 365);

				setcookie('remember', '', time() - 3600 * 24 * 365);

			}  
                $this -> redirect('UserMember/userlist');die;
        }
            
		//没有这行代码会造成$is_login为null，从而造成session过期之后cookie被删除，自动登录失效。
            //不加这个判断，退出登录之后会造成老师和学生列表空
            if(!empty($_COOKIE['username'])){
                D('UserMember')->is_login($_COOKIE['username']);
                $is_login = session('is_login');
            }
            $data = D('UserMember') ->updateLogin(id,session('uid'));
//            dump($is_login);exit();
            if($is_login != $data){
                        session(null);
                        $url = $_SERVER['HTTP_REFERER'];
                        // dump($url);die;
                        setcookie('username',$user['username'],time()-3600*24*365);
                        setcookie('password',$post['password'],time()-3600*24*365);



                        setcookie('remember',$post['remember'],time()-3600*24*365);



                        header('Location:'.$url);die;



               $this -> redirect($url);die;



            }



            



        }



        



    }





        //自动登录
        /**
     * 课时三天后自动确认
     * @author qlj
     */

    public function autoConfirm(){
        $ids = D('CourseRecord') -> endClass();
        if (!empty($ids)) {
            $mycourse = D('Mycourse');
            foreach ($ids as $v) {
                if ($mycourse -> updateSalary($v['id'],$v['order'])) {
                    $mycourse -> mystuAjaxStu($v['id'],$v['order']);
                }
            }
        }
    }


	/* 用户登录检测 */



	protected function logins(){



         if (!is_login()) {



            $this -> redirect('UserMember/login');die;



            //$this -> display('UserMember/login');die;



            //header('Location:'.U('UserMember/login'));



         }



        /* 用户登录检测 */



        // || $this ->display();



		 $this->error('您还没有登录，请先登录！', U('UserMember/login'),0);



	}







}



